//
//  ECTestViewController.m
//  ECDectorIOSSDK
//
//  Created by 黄志强 on 09/15/2022.
//  Copyright (c) 2022 黄志强. All rights reserved.
//

#import "ECTestViewController.h"
#import "ECDectorSDK.h"

@interface ECTestViewController () <ECViewControllerDelegate>
@property (nonatomic, strong) UITextField *tfIdCard;
@property (nonatomic, strong) UITextField *tfParam;
@property (nonatomic, strong) UITextField *tfFace;
@end

@implementation ECTestViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    CGSize size = UIScreen.mainScreen.bounds.size;
    
    
    UITextField *tf = [[UITextField alloc] init];
    tf.text = @"";
    tf.textColor = UIColor.blackColor;
    tf.textAlignment = NSTextAlignmentCenter;
    tf.attributedPlaceholder=[[NSMutableAttributedString alloc]initWithString:@"请输入身份证识别地址（有默认值）"attributes:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
    [self.view addSubview:tf];
    tf.frame = CGRectMake(20, 160, size.width-40, 36);
    self.tfIdCard = tf;
    
    UIButton *btn1 = [[UIButton alloc] init];
    [btn1 setTitle:@"首页" forState:UIControlStateNormal];
    [btn1 setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    btn1.frame = CGRectMake((size.width-150)/2, 200, 150, 36);
    btn1.layer.borderColor = UIColor.blackColor.CGColor;
    btn1.layer.borderWidth = 1;
    [btn1 addTarget:self action:@selector(handle1) forControlEvents:UIControlEventTouchUpInside];
    
    
    UITextField *tf2 = [[UITextField alloc] init];
    tf2.textColor = UIColor.blackColor;
    tf2.text = @"";
    tf2.textAlignment = NSTextAlignmentCenter;
    tf2.attributedPlaceholder=[[NSMutableAttributedString alloc]initWithString:@"请输入人脸识别地址（有默认值）"attributes:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
    [self.view addSubview:tf2];
    tf2.frame = CGRectMake(20, 360, size.width-40, 36);
    self.tfFace = tf2;
    
    UITextField *tf3 = [[UITextField alloc] init];
    tf3.textColor = UIColor.blackColor;
    tf3.textAlignment = NSTextAlignmentCenter;
    tf3.attributedPlaceholder=[[NSMutableAttributedString alloc]initWithString:@"请输入key value参数（以分号间隔）"attributes:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
    [self.view addSubview:tf3];
    tf3.frame = CGRectMake(20, 290, size.width-40, 36);
    self.tfParam = tf3;
    
    
    UIButton *btn2 = [[UIButton alloc] init];
    [btn2 setTitle:@"人脸登录" forState:UIControlStateNormal];
    [btn2 setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    btn2.frame = CGRectMake((size.width-150)/2, 440, 150, 36);
    btn2.layer.borderColor = UIColor.blackColor.CGColor;
    btn2.layer.borderWidth = 1;
    [btn2 addTarget:self action:@selector(handle2) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handle1 {
    /// exinfo：token
    /// http://railtraffic.zaibinzhang.com/register/idcard
    /// http://railtraffic.zaibinzhang.com/test
    NSArray *list = [self.tfParam.text componentsSeparatedByString: @";"];
    NSMutableDictionary *param = @{}.mutableCopy;
    NSString *key = @"";
    NSString *value = @"";
    for (NSString *tmp in list) {
        if (key.length <= 0) {
            key = tmp;
        } else {
            value = tmp;
        }
        if (key.length > 0 && value.length > 0) {
            param[key] = value;
            key = @"";
            value = @"";
        }
    }
    ECViewController *vc = [[ECViewController alloc] initWithURL: @"http://railtraffic.zaibinzhang.com" exinfo:param];
    vc.ecdelegate = self;
    [self.navigationController pushViewController:vc animated:YES];
//    [self presentViewController:vc animated:YES completion:nil];
    if (self.tfIdCard.text.length > 0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc loadUrl:self.tfIdCard.text];
        });
    }
}

- (void)handle2 {
    NSArray *list = [self.tfParam.text componentsSeparatedByString: @";"];
    NSMutableDictionary *param = @{}.mutableCopy;
    NSString *key = @"";
    NSString *value = @"";
    for (NSString *tmp in list) {
        if (key.length <= 0) {
            key = tmp;
        } else {
            value = tmp;
        }
        if (key.length > 0 && value.length > 0) {
            param[key] = value;
            key = @"";
            value = @"";
        }
    }
    ECViewController *vc = [[ECViewController alloc] initWithURL: @"http://railtraffic.zaibinzhang.com/face/login" exinfo:param];
    vc.ecdelegate = self;
    [self.navigationController pushViewController:vc animated:YES];
    if (self.tfFace.text.length > 0) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc loadUrl:self.tfFace.text];
        });
    }
}

- (void)viewController:(ECViewController *)viewController didFinishLoginWithInfo:(NSDictionary<NSString *, NSString *> *)info {
    NSLog(@"---- ：%@", info);
    [viewController.navigationController popViewControllerAnimated:YES];
}
@end
